<?php 
// Library files
include 'includes/cPlayer.php';
include 'includes/cMember.php';
include 'includes/cDatabase.php';
include 'includes/variables.php';
include 'includes/cPage.php';
include 'includes/cTransfer.php';
include 'includes/cDates.php';

$cPage = new page("Team Stats");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

define("TD_DEFAULT", "<td rowspan='n' colspan='n' width='n' align='center|right' valign='top|bottom' bgcolor='n'>");
define("TD_JPEG", "<td width='50' align='center'>");
define("TD_POSITION", "<td width='50' align='center'>");
define("TD_CODE", "<td width='30' align='center'>");
define("TD_NAME", "<td width='100' align='left'>");
define("TD_TEAM", "<td width='10' align='center'>");
define("TD_VALUE", "<td width='10' align='left'>");
define("TD_SCORE", "<td width='50' align='center'>");
define("TD_HEADING", "<td rowspan='1' colspan='8' align='center' valign='top'>");
define("TD_CAT", "<td width='200' align='left'>");

if(@$_GET['form'] == "yes")
{
	$week = $_POST['selectWeek'];
}
else
{
	$week = 1;
}
		
$error = 0;
$j = 0;
$k = 0;
	
$cPlayer = array();
$currPlayerCodeList = array();
	
$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
$db = $cDatabase->selectDatabase($connect, $activeDatabase);

$maxWeeks = $cDatabase->GetMaxWeeks($connect);
$captainsEnabled = $cDatabase->GetCaptainsEnabled($connect);

$selectArray[0] = '*';
$result = $cDatabase->selectFromDatabase($connect, 'memberlist', $selectArray, 'memberName', $memberName, NULL, NULL);

$finfo = mysqli_fetch_fields($result);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	
// Get all the data for this member and put it in the class
$cMember = new member;

$error |= $cMember->setMemberName($row['memberName']);
$error |= $cMember->setTeamName($row['teamName']);
$error |= $cMember->setMoneyPaid($row['moneyPaid']);
$error |= $cMember->setActive($row['active']);
$error |= $cMember->setMiniPop($row['miniPop']);
$cMember->loadCaptainArray($row);
	
for ($i = 1; $i <= MAXPLAYERS; $i++)
{
	$playerCodeVariable = getPlayerCodeVariable($i);
	$playerWeekInVariable = getPlayerWeekInVariable($i);
	$playerWeekOutVariable = getPlayerWeekOutVariable($i);

	if ($row[$playerCodeVariable] != NULL)
	{
		$cPlayer[$i] = new player($row[$playerCodeVariable]);
		$error |= $cPlayer[$i]->setPlayerWeekIn($row[$playerWeekInVariable]);
		$error |= $cPlayer[$i]->setPlayerWeekOut($row[$playerWeekOutVariable]);
		$error |= $cPlayer[$i]->setPlayerName($connect);
		$error |= $cPlayer[$i]->setPlayerTeam($connect);
		$error |= $cPlayer[$i]->setPlayerPosition($connect);
		$error |= $cPlayer[$i]->setPlayerValue($connect);
		$error |= $cPlayer[$i]->setPlayerActive($connect);

		if ($cPlayer[$i]->getPlayerCode() != NULL)							// Is this a valid player
		{
			$cPlayer[$i]->calculatePlayerScore($connect);
			$cMember->setScoresForEachWeek($cPlayer[$i]);
			if ($cPlayer[$i]->getPlayerWeekIn() <= $week)
			{
				if ( ($cPlayer[$i]->getPlayerWeekOut() > $week) || ($cPlayer[$i]->getPlayerWeekOut() == NULL) )
				{
					$currPlayerCodeList[$k] = $cPlayer[$i]->getPlayerCode();	// Put it into the curr array
					$k++;
					$error |= $cMember->addPlayerValue($cPlayer[$i]->getPlayerValue());
					if ($captainsEnabled == 0)
					{
						$playerScore = $cPlayer[$i]->getPlayerScore($maxWeeks);
					}
					else
					{
						$captainForWeek = array();
						$captainForWeek = loadCaptainForWeekArray($captainForWeek, $maxWeeks, $cPlayer[$i], $cMember);
						$playerScore = $cPlayer[$i]->getPlayerScoreCaptains($maxWeeks, $captainForWeek);
					}
					$error |= $cMember->addPlayerScore($playerScore, 'CURR');
					$error |= $cMember->addFormation( $cPlayer[$i]->getPlayerPosition() );
				}
			}
		}
	}
}
sort($currPlayerCodeList);

$cTransfer = new transfer();
$currentCaptain = $cTransfer->getCaptainCodeForWeek($cMember->getTeamName(), $week);
displayTeam($cPlayer, $cMember, $playerCodes, $currPlayerCodeList, $week, $maxWeeks, $captainsEnabled, $currentCaptain);

function loadCaptainForWeekArray($captainForWeek, $maxWeeks, $cPlayer, $cMember)
{
	for ($week = 1; $week <= $maxWeeks; $week++)
	{
		// See if this playercode is in the list of captains per week
		if ($cPlayer->getPlayerCode() == $cMember->getCaptainForWeek($week))
		{
			$captainForWeek[$week] = 1;
		}
		else
		{
			$captainForWeek[$week] = 0;
		}
	}
	
	return $captainForWeek;
}

function displayTeam($cPlayer, $cMember, $playerCodes, $currPlayerCodeList, $week, $maxWeeks, $captainsEnabled, $currentCaptain)
{
	$PHP_SELF = $_SERVER['PHP_SELF'];
	$k = 0;

	echo "<table width='1000' border='1'>";
		echo "<tr>";
			echo "<td rowspan='12' colspan='8'>";
				echo "<table>";
					for ($i = 0; $i < 11; $i++)
					{
						for ($l = 1; $l <= MAXPLAYERS; $l++)
						{
							if (($cPlayer[$l]->getPlayerCode() == $currPlayerCodeList[$i]) && ($cPlayer[$l]->getPlayerWeekOut() == NULL))
							{	// We've found the player in the cPlayer structure
								break;
							}
						}
						echo "<tr>";
							// This player is in the current lineup
							if ($cPlayer[$l]->getPlayerCode() == $currentCaptain)
							{
								echo TD_JPEG."<img src='JPEGs/captain.jpg' width='50' height='50'></td>";
							}
							else
							{
								$path = $cPlayer[$l]->shirtLookup();
								echo TD_JPEG."<img src='".$path."' width='50' height='50'></td>";
							}
							echo TD_POSITION.BLUE.$cPlayer[$l]->getPlayerPosition()."</td>";	// Position
							echo TD_CODE.BLUE.$cPlayer[$l]->getPlayerCode()."</td>";			// Code
							echo TD_NAME.BLUE.$cPlayer[$l]->getPlayerName()."</td>";			// Name
							echo TD_TEAM.BLUE.$cPlayer[$l]->getPlayerTeam()."</td>";			// Team
							echo TD_VALUE.BLUE.$cPlayer[$l]->getPlayerValue()."m</td>";			// Value
							if ($captainsEnabled == 0)
							{
								$playerScoreForWeek = $cPlayer[$l]->getPlayerScoreForWeek($week);
							}
							else
							{
								$captainForWeek = array();
								$captainForWeek = loadCaptainForWeekArray($captainForWeek, $maxWeeks, $cPlayer[$l], $cMember);
								$playerScoreForWeek = $cPlayer[$l]->getPlayerScoreForWeekCaptains($week, $captainForWeek);
							}
							if ($playerScoreForWeek == NULL)
								echo TD_SCORE.BLUE."-</font></td>";	// Score
							else
								echo TD_SCORE.BLUE.$playerScoreForWeek."</font></td>";	// Score			
							echo "<td>&nbsp;</td>";
						echo "</tr>";
					}
						echo "<tr>";
							echo "<td></td>";
						?>
							<td colspan='3'>
							<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=yes&memberName=<?php echo $cMember->getMemberName() ?>" method="POST">
							<table border='0'>
								<tr>
									<td colspan="2" align="center">
									<input name="selectWeekButton" type="submit" id="selectWeek" value="Select Week">
						    		<select name="selectWeek">
									<?php
									echo"<option value = '" .$week. "'>" .$week. "</option>";
									for ($j = 1; $j <= $maxWeeks; $j++)
									{
					    		  		echo"<option value = '" .$j. "'>" .$j. "</option>";
									}
									?>
							       	</select>
									</td>
								</tr>
							</table>
							</form>
							</td>
						<?php
						echo "<td colspan='2'>WEEK ".$week." POINTS</td>";
						echo TD_SCORE."<strong>".$cMember->getScoreForWeek($week)."</strong></td>";
					echo "</tr>";
				echo "</table>";
			echo "</td>";
			echo "<td>";
				echo "<table>";
					echo "<td><img src='JPGraph\DB_TeamStatsWeeklyScoresGraph?memberName=".$cMember->getMemberName()."' border=0 align=center width=500 height=300></td>";
				echo "</table>";
			echo "</td>";
		echo "</tr>";
	
	// Other information table
	echo "<tr>";
		echo "<td><table>";
			echo "<td><img src='JPGraph\DB_TeamStatsWeeklyPositionGraph?memberName=".$cMember->getMemberName()."' border=0 align=center width=500 height=300></td>";
		echo "</table></td>";
	echo "</tr>";
	
	echo "</table>";

}
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>